package com.example.demo.service;

import com.example.demo.bean.Cat;
import com.example.demo.dao.CatDao;
import com.example.demo.repository.ICat2Repository;
import com.example.demo.repository.ICatRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;

/**
 * Created by ${lgs} on 2018/7/24
 */

@Service
public class CatService {

    @Resource
    private ICatRepository catRepository;

    @Resource
    private ICat2Repository cat2Repository;

    @Resource
    private CatDao catDao;

    /**
     * save,update,delete方法需要绑定事务
     * <p>
     * 使用@Transactional进行事务的绑定
     *
     * @param cat
     */

    //保存数据
    @Transactional
    public void save(Cat cat) {
        catRepository.save(cat);
    }

    //删除数据
    @Transactional
    public void delete(int id) {
        catRepository.deleteById(id);
    }

    //查询数据(getAll()是一个只读查询，不需要添加事务)
    public Iterable<Cat> getAll() {
        return catRepository.findAll();
    }

    public Cat findByCatName(String catName) {
        return cat2Repository.findByCatName(catName);
    }

    public Cat findByCatName2(String catName) {
        return cat2Repository.findByCatName2(catName);
    }

    public Cat selectByCatName(String catName) {
        return catDao.selectByCatName(catName);
    }
}
